﻿@using MvcApplicationTest.Controllers
@{
    ViewBag.Title = "Change User's Balance";
}

<h2>@ViewBag.Title.</h2>
<p>
    <input type="search" name="InputName" value="Input Name" />
    <input type="button" value="Search" onclick="Search_OnClick()" />
    <input type="button" id="ShowAll" value="Show All User" onclick="ShowAllUser()" />
    <div id="SearchResult" style="display: none">
    </div>
    <div id="ChangeButton" style="display: none">
        <input type="button" value="Change Now" onclick="ChangeBtn_OnClick()">
    </div>
</p>

<aside>
    <h3>Return:</h3>
    <ul>
        <li>@Html.ActionLink("Change user balance", "ChangeUserBalance", "Home")</li>
        <li>@Html.ActionLink("Show today's orders", "ShowOrdersOfToday", "Home")</li>
        <li>@Html.ActionLink("Show all users' information", "ShowAllUser", "Home")</li>
    </ul>
</aside>



<script type="text/javascript">
    function Search_OnClick() {
        var $input = $("input[name=InputName]");
        var str = $input.val();
        $("#SearchResult").css("display", "block");
        $("#ChangeButton").css("display", "block");

        $.ajax({
            type: "POST",
            url: "/Home/AdminManageSearchResult",
            datatype: "json",
            data: { "str": str },
            async: false,
            success: function (result) {
                if (result.toString() == "false") {
                    alert("Please input name.");
                }
                else {
                    var $div = $("#SearchResult");
                    $div.text("");
                    $div.append(result);
                    $("input[name=UserSearchResult]").css("width", "100px")
                    //var htmlStr = JSON.parse(result);
                    //给页面添加记录
                    //window.location.href = "../../Home/AdminManager";
                }
            },
            error: function (error) {
            }
        });
    }

    function ShowAllUser() {
        $("#SearchResult").css("display", "block");
        $("#ChangeButton").css("display", "block");
        var str = "Show All User";
        $.ajax({
            type: "POST",
            url: "/Home/AdminManageSearchResult",
            datatype: "json",
            data: { "str": str },
            async: false,
            success: function (result) {
                var $div = $("#SearchResult");
                $div.text("");
                $div.append(result);
                $("input[name=UserSearchResult]").css("width", "100px")
                //var htmlStr = JSON.parse(result);
                //给页面添加记录
                //window.location.href = "../../Home/AdminManager";
            },
            error: function (error) {
            }
        });
    }

    function ChangeBtn_OnClick() {
        var $results = $("input[name=UserSearchResult]");
        var obj = [];
        for (var i = 0; i < $results.length; i++) {
            obj.push($($results[i]).attr("username") + "|" + $($results[i]).attr("value"));
        }
        var str = obj.toString();
        $.ajax({
            type: "POST",
            url: "/Home/AdminManageBalanceChanged",
            datatype: "json",
            data: { "str": str },
            async: false,
            success: function (result) {
                if (result.toString() == "true")
                    alert("Change balance ok.");
                else if (result.toString() == "false")
                    alert("Change error.");
            },
            error: function (error) {
            }
        });
    }
</script>
